<%
# encoding: utf-8
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2011    Vít Jonáš <vit.jonas@gmail.com>
# Copyright (C) 2012    Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2011-15 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<div class="box">
  <%= form_tag({:controller => 'dmsf_upload', :action => 'upload_files', :id => @project, :folder_id => @folder},
    :id => 'uploadform', :method=>:post, :multipart => true) do %>
    <% if Setting.attachment_max_size.to_i >= 102400 %> 
      <div class="upload_select">
	<%= l(:label_file_size) %>: 
          <select id="uploader_select">
           <option value="1">
              &lt; <%= "#{@ajax_upload_size}" %> MB
            </option>
            <option value="2">
              > <%= "#{@ajax_upload_size}" %> MB
            </option>
          </select>
      </div>
    <% end %>
    <h3><%= l(:heading_file_upload) %></h3>
    <div style="padding-bottom: 6px; line-height: 1.4em; font-size: 0.9em;">
      <span>
        <% max_file_upload = Setting.plugin_redmine_dmsf['dmsf_max_file_upload'].to_i %>
        <%= l(:note_uploaded_maximum_files_at_once, :number => max_file_upload) if max_file_upload > 0 %>
        <%= l(:note_upload_files_greater_than_two_gb) if Setting.attachment_max_size.to_i >= 2097151 %>
      </span>
    </div>
    <div id="uploader">      
      <p>
        <label><%=l(:label_attachment_plural)%></label>
        <%= render :partial => 'attachments/form' %>        
      </p>        
      <%= submit_tag(l(:submit_upload_files)) %>
    </div>    
  <% end %>
</div>

<script type="text/javascript">
  var originalUploaderContent;
  var uploader = jQuery('#uploader');

  originalUploaderContent = uploader.html();

  jQuery('#uploader_select').change(function() {
    if(jQuery(this).val() == 2) {
      uploader.html(originalUploaderContent);
    } else {
      initPlUploader(uploader);
    }
  });

  initPlUploader(uploader);

  var dmsfFileFieldCount = 1; 
</script>

<% content_for :header_tags do %>
  <%= stylesheet_link_tag 'plupload/jquery.ui.plupload.css', :plugin => 'redmine_dmsf' %>  
  <%= javascript_include_tag 'plupload/js/plupload.full.min.js', :plugin => 'redmine_dmsf' %>
  <%= javascript_include_tag 'plupload/js/jquery.ui.plupload/jquery.ui.plupload.js', :plugin => 'redmine_dmsf' %>
  <%= javascript_include_tag("plupload/js/i18n/#{I18n.locale.to_s.downcase}.js", :plugin => 'redmine_dmsf') if I18n.locale && !I18n.locale.to_s.match(/^en.*/) %>

  <script type="text/javascript">
    function initPlUploader(uploader) {
      uploader.html('<div></div>');
      uploader = jQuery('div', uploader);
      uploader.plupload({
        runtimes : 'html5,flash,silverlight,html4',
        url : '<%= url_for({:controller => 'dmsf_upload', :action => 'upload_file', :id => @project}) %>',
        max_file_size : '<%= "#{@ajax_upload_size}mb" %>',
        max_file_count: '<%= Setting.plugin_redmine_dmsf['dmsf_max_file_upload'].to_i if Setting.plugin_redmine_dmsf['dmsf_max_file_upload'].to_i > 0 %>',
        multipart: true,
        // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
        dragdrop: true,
        multipart_params : {authenticity_token : jQuery('input[name=authenticity_token]').val()},

        // Rename files by clicking on their titles
        rename: true,
        
        // Views to activate
        views: {
            list: true,
            thumbs: true, // Show thumbs
            active: 'thumbs'
        },
        
        // Flash settings
        flash_swf_url : '<%= plugin_asset_path(:redmine_dmsf, 'javascripts', 'plupload/js/Moxie.swf') %>',

        // Silverlight settings
        silverlight_xap_url : '<%= plugin_asset_path(:redmine_dmsf, 'javascripts', 'plupload/js/Moxie.xap') %>'
      });
      jQuery('.plupload_scroll',uploader).resizable({
        handles: 's'
      });
      var pluploader = uploader.plupload('getUploader');

      pluploader.bind('FileUploaded', function(pluploader, file, response) {
        var responseObject = jQuery.parseJSON(response.response);
        if (responseObject == null) { //Bug: on Firefox folders entries act unexpectedly.
          file.status = plupload.FAILED;
          pluploader.trigger('UploadProgress', file);
          pluploader.trigger('QueueChanged');
        } else {
          if (responseObject.error == null) {
            var disk_filename_input = jQuery('<input/>').attr('type', 'hidden')
                    .attr('name', 'uploaded[' + dmsfFileFieldCount + '][disk_filename]')
                    .val(responseObject.disk_filename);
            uploader.append(disk_filename_input);
            var content_type_input = jQuery('<input/>').attr('type', 'hidden')
                    .attr('name', 'uploaded[' + dmsfFileFieldCount + '][content_type]')
                    .val(responseObject.content_type);
            uploader.append(content_type_input);
            var original_filename_input = jQuery('<input/>').attr('type', 'hidden')
                    .attr('name', 'uploaded[' + dmsfFileFieldCount + '][original_filename]')
                    .val(responseObject.original_filename);
            uploader.append(original_filename_input);
          } else {
              file.status = plupload.FAILED;              
              pluploader.trigger('UploadProgress', file);
              pluploader.trigger('QueueChanged');             
          }
        }
        if(pluploader.total.uploaded == pluploader.files.length) jQuery('#uploadform').submit();
        else if((pluploader.total.uploaded + pluploader.total.failed) == pluploader.files.length) setTimeout(function() {jQuery('#uploadform').submit();}, 2000);
        else dmsfFileFieldCount++;
        return true;
      });
    }
  </script>
<% end %>